import * as THREE from 'three'
import { camera, scene } from './index'

/**
 * @param data 场景2中的点击事件，目前没用到
 */
// const handleLabel = (data: any) => {
//   console.log(data);
// };

/**
 * 通过射线与对象相交来获取对象
 * @param event 鼠标滑过获取的对象
 * @param container dom对象
 * @returns 获取地图上的对象
 */
const getIntersects = (event: any, container: any): any => {
  if (!container) return
  const { left, top } = container.getBoundingClientRect()
  const { offsetWidth, offsetHeight } = container
  // 创建射线
  const raycaster = new THREE.Raycaster()
  // 创建一个二维向量的对象
  const mouse = new THREE.Vector2()
  mouse.x = ((event.clientX - left) / offsetWidth) * 2 - 1
  mouse.y = -((event.clientY - top) / offsetHeight) * 2 + 1
  raycaster.setFromCamera(mouse, camera)
  const intersects = raycaster.intersectObject(scene, true)
  return intersects
}

/**
 * @param object 传入地球模型
 * @returns 返回的是省份
 */
const getEventType = (object: any) => {
  if (object?.name?.indexOf('地球') > -1) {
    return { value: {}, name: '地球' }
  } else if (
    object?.name?.indexOf('省面') > -1 ||
    object?.name?.indexOf('省界') > -1 ||
    object?.name?.indexOf('透明') > -1
  ) {
    const province = object.name.split('-')[0]
    return {
      name: '省份',
      value: {
        province: `${province}省`
      }
    }
  } else {
    return {
      value: {},
      name: '背景'
    }
  }
}

export { getIntersects, getEventType }
